<?php
   class Payment extends MY_Controller{
	   public function __construct(){
		   parent::__construct();
		   $this->load->helper("url");
		   $this->load->model("model_pay");
		   $this->load->library('session');
		   $this->load->library("pager");
	   }
	   public function index(){
		   $data['error'] = "";
		   $data['title'] = "Xác nhận đặt phòng";
		   $data['config']	= $this->config();
		   $data['gold']		= $this->goldtime();
		   if($this->input->post()){
			   $date = $this->input->post("startdate");
			   $startdate = substr($date,0,2);
			   $startmon  = substr($date,3,5);
			   $type	 = $this->input->post("room_type");
			   $room_now = $this->input->post("room_now");
			   $day_num  = $this->input->post("day_num");
			   $room_number = $this->input->post("room");
			   $total_price = $this->input->post("price");
			   $adults		=	$this->input->post("adults");
			   $child05		= $this->input->post("children");
			   $child51		= $this->input->post("childr");
			   $price_type  = $this->input->post("price_type");
			   $price52 = $child51*1250000;
		   	   if($type == 1){
				   $price_total = $price_type + 2250000;
				   $monney = (4500000*($room_number - 1)) + $price_total;
				   $monney = $monney + $price52;
			   }else{
				   $monney = ($price_type*$room_number) + $price52;
			   }
			   if($monney != $total_price){
				   redirect(base_url()."home/payment/failed");
			   }else{
				   $data['date'] = $date;
				   $data['startdate'] = $startdate;
				   $data['startmon'] = $startmon;
				   $data['type'] = $type;
				   $data['room_now'] = $room_now;
				   $data['room_tick'] = $day_num;
				   $data['room_number'] = $room_number;
				   $data['total_price'] = $monney;
				   $data['adults_num'] = $adults;
				   $data['child05'] = $child05;
				   $data['child51'] = $child51;
			   	   $this->load->view("payment/layout",$data);
			   }
		   }else{
			  	redirect(base_url());
		   	   //$this->load->view("payment/layout",$data);
		   }
	   }
	   public function process(){
		   if(!$this->input->post()){
			   redirect(base_url()."home/payment/failed");
		   }else{
			   $random = "PARADISE".$this->ran().date('His').rand();
			   $db = array(
			   		"order_name"		=> $this->fillter($this->input->post("order_name")),
					"order_code"		=> $random,
					"order_email"		=> $this->fillter($this->input->post("order_email")),
					"order_phone"		=> $this->fillter($this->input->post("order_phone")),
					"order_add"			=> $this->fillter($this->input->post("order_add")),
					"order_cmnd"		=> $this->fillter($this->input->post("order_cmnd")),
			   		"order_type"		=> $this->fillter($this->input->post("order_type")),
			   		"order_startdate" 	=> substr($this->input->post("order_date"),0,2),
					"order_startmon"	=> substr($this->input->post("order_date"),3,5),
					"order_roomnow"		=> $this->input->post("order_roomnow"),
					"order_roomnumber"	=> $this->input->post("order_roomnumber"),
					"order_price"		=> $this->input->post("order_price"),
					"order_adults"		=> $this->input->post("order_adults"),
					"order_child05"		=> $this->input->post("order_child05"),
					"order_child51"		=> $this->input->post("order_child51"),
					"order_date"		=> date("d/m/Y - H:i:s")
			  	);
				$session = array(
					"order"		=> $random,
					"type"		=> $db['order_type'],
					"startdate" => $db['order_startdate'],
					"startmont" => $db['order_startmon'],
					"room_now"	=> $db['order_roomnow'],
					"room_next" => $db['order_roomnext'],
					"room_num" 	=> $db['order_roomnumber'],
					"room_tick"	=> $this->input->post("order_tick")
				);
				$this->session->set_userdata($session);
				$onepay = array(
					"virtualPaymentClientURL" => "http://mtf.onepay.vn/onecomm-pay/vpc.op",
					"Title" => "VPC 3-Party",
					"vpc_AccessCode" => "D67342C2",
					"vpc_Amount"	=> ($db['order_price']*100),
					"vpc_Command"	=> "pay",
					"vpc_Currency"	=> "VND",
					"vpc_Locale"	=> "vn",
					"vpc_MerchTxnRef"	=> date('YmdHis').rand(),
					"vpc_Merchant"	=> "ONEPAY",
					"vpc_OrderInfo"	=> $random,
					"vpc_ReturnURL"	=> base_url()."home/pay_result/index",
					"vpc_TicketNo"	=> $_SERVER ['REMOTE_ADDR'],
					"vpc_Version"	=> "2"
				);
				$this->model_pay->add($db);
				$url = $this->onepay($onepay);
				redirect($url);
		   }
	   }
	   public function timeg(){
		   if($this->session->userdata("gold_true") != "true"){
			   redirect(base_url());
		   }
		   $data['error'] = "";
		   $data['title'] = "Xác nhận đặt phòng";
		   $data['config']	= $this->config();
		   $data['gold']		= $this->goldtime();
		   if($this->input->post()){
			   $date = $this->input->post("startdate");
			   $startdate = substr($date,0,2);
			   $startmon  = substr($date,3,5);
			   $type	 = $this->input->post("room_type");
			   $room_now = $this->input->post("room_now");
			   $room_next = $this->input->post("room_next");
			   $room_number = $this->input->post("room");
			   $total_price = $this->input->post("price");
			   $adults		=	$this->input->post("adults");
			   $child05		= $this->input->post("children");
			   $child51	= $this->input->post("childr");
			   $pri = $this->session->userdata("gold_price");
		   	   if($type == 1){
				   $price = $pri+2250000;
			   }else{
				   $price = $pri+4500000;
			   }
			   $price_child = $child51*1250000;
			   //$price_child_per = (55*$price_child)/100;
			   $price_php = ($room_number*$price) + $price_child;
			   if($price_php != $total_price){
				   redirect(base_url()."home/payment/failed");
			   }else{
				   $data['date'] = $date;
				   $data['startdate'] = $startdate;
				   $data['startmon'] = $startmon;
				   $data['type'] = $type;
				   $data['room_now'] = $room_now;
				   $data['room_next'] = $room_next;
				   $data['room_number'] = $room_number;
				   $data['total_price'] = $price_php;
				   $data['adults_num'] = $adults;
				   $data['child05'] = $child05;
				   $data['child51'] = $child51;
			   	   $this->load->view("payment/gold/layout",$data);
			   }
		   }else{
			  	redirect(base_url());
		   	   //$this->load->view("payment/layout",$data);
		   }
	   }
	   public function timeg_process(){
		   if(!$this->input->post()){
			   redirect(base_url()."home/payment/failed");
		   }else{
			   $random = "PARADISE".$this->ran().date('His').rand();
			   $db = array(
			   		"order_name"		=> $this->fillter($this->input->post("order_name")),
					"order_code"		=> $random,
					"order_email"		=> $this->fillter($this->input->post("order_email")),
					"order_phone"		=> $this->fillter($this->input->post("order_phone")),
					"order_add"			=> $this->fillter($this->input->post("order_add")),
					"order_cmnd"		=> $this->fillter($this->input->post("order_cmnd")),
			   		"order_type"		=> $this->fillter($this->input->post("order_type")),
			   		"order_startdate" 	=> substr($this->input->post("order_date"),0,2),
					"order_startmon"	=> substr($this->input->post("order_date"),3,5),
					"order_roomnow"		=> $this->input->post("order_roomnow"),
					"order_roomnext"	=> $this->input->post("order_roomnext"),
					"order_roomnumber"	=> $this->input->post("order_roomnumber"),
					"order_price"		=> $this->input->post("order_price"),
					"order_adults"		=> $this->input->post("order_adults"),
					"order_child05"		=> $this->input->post("order_child05"),
					"order_child51"		=> $this->input->post("order_child51"),
					"order_date"		=> date("d/m/Y - H:i:s"),
					"order_gold_type"  	=> $this->session->userdata("gold_type"),
					"order_gold_ip"		=> $_SERVER ['REMOTE_ADDR']		
			  	);
				$session = array(
					"order"		=> $random,
					"type"		=> $db['order_type'],
					"startdate" => $db['order_startdate'],
					"startmont" => $db['order_startmon'],
					"room_now"	=> $db['order_roomnow'],
					"room_next" => $db['order_roomnext'],
					"room_num" => $db['order_roomnumber']
				);
				$this->session->set_userdata($session);
				$onepay = array(
					"virtualPaymentClientURL" => "http://mtf.onepay.vn/onecomm-pay/vpc.op",
					"Title" => "VPC 3-Party",
					"vpc_AccessCode" => "D67342C2",
					"vpc_Amount"	=> ($db['order_price']*100),
					"vpc_Command"	=> "pay",
					"vpc_Currency"	=> "VND",
					"vpc_Locale"	=> "vn",
					"vpc_MerchTxnRef"	=> date('YmdHis').rand(),
					"vpc_Merchant"	=> "ONEPAY",
					"vpc_OrderInfo"	=> $random,
					"vpc_ReturnURL"	=> base_url()."home/pay_result/goldtime",
					"vpc_TicketNo"	=> $_SERVER ['REMOTE_ADDR'],
					"vpc_Version"	=> "2"
				);
				$this->model_pay->add_gold($db);
				$url = $this->onepay($onepay);
				redirect($url);
		   }
	   }
	   public function onepay($data){
			// Khóa bí mật - được cấp bởi OnePAY
			$SECURE_SECRET = "A3EFDFABA8653DF2342E8DAC29B51AF0";
			
			// add the start of the vpcURL querystring parameters
			// *****************************Lấy giá trị url cổng thanh toán*****************************
			$vpcURL = $data["virtualPaymentClientURL"] . "?";
			
			// Remove the Virtual Payment Client URL from the parameter hash as we 
			// do not want to send these fields to the Virtual Payment Client.
			// bỏ giá trị url và nút submit ra khỏi mảng dữ liệu
			unset($data["virtualPaymentClientURL"]); 
			unset($data["SubButL"]);
			
			//$stringHashData = $SECURE_SECRET; *****************************Khởi tạo chuỗi dữ liệu mã hóa trống*****************************
			$stringHashData = "";
			// sắp xếp dữ liệu theo thứ tự a-z trước khi nối lại
			// arrange array data a-z before make a hash
			ksort ($data);
			// set a parameter to show the first pair in the URL
			// đặt tham số đếm = 0
			$appendAmp = 0;
			
			foreach($data as $key => $value) {
			
				// create the md5 input and URL leaving out any fields that have no value
				// tạo chuỗi đầu dữ liệu những tham số có dữ liệu
				if (strlen($value) > 0) {
					// this ensures the first paramter of the URL is preceded by the '?' char
					if ($appendAmp == 0) {
						$vpcURL .= urlencode($key) . '=' . urlencode($value);
						$appendAmp = 1;
					} else {
						$vpcURL .= '&' . urlencode($key) . "=" . urlencode($value);
					}
					//$stringHashData .= $value; *****************************sử dụng cả tên và giá trị tham số để mã hóa*****************************
					if ((strlen($value) > 0) && ((substr($key, 0,4)=="vpc_") || (substr($key,0,5) =="user_"))) {
						$stringHashData .= $key . "=" . $value . "&";
					}
				}
			}
			//*****************************xóa ký tự & ở thừa ở cuối chuỗi dữ liệu mã hóa*****************************
			$stringHashData = rtrim($stringHashData, "&");
			// Create the secure hash and append it to the Virtual Payment Client Data if
			// the merchant secret has been provided.
			// thêm giá trị chuỗi mã hóa dữ liệu được tạo ra ở trên vào cuối url
			if (strlen($SECURE_SECRET) > 0) {
				//$vpcURL .= "&vpc_SecureHash=" . strtoupper(md5($stringHashData));
				// *****************************Thay hàm mã hóa dữ liệu*****************************
				$vpcURL .= "&vpc_SecureHash=" . strtoupper(hash_hmac('SHA256', $stringHashData, pack('H*',$SECURE_SECRET)));
			}
			
			// FINISH TRANSACTION - Redirect the customers using the Digital Order
			// ===================================================================
			// chuyển trình duyệt sang cổng thanh toán theo URL được tạo ra
			//echo $vpcURL;
			//die();
			return $vpcURL;
	   }
	   public function failed(){
		    $data['title'] = "Failed booking";
			$data['config'] 		= $this->config();
			$data['input'] = "Đã xảy ra lỗi,xin vui lòng thao tác lại!";
			$this->load->view("payment/failed/layout",$data);
	   }
	   public function ran(){
			$input = array("a","b","c","d","e","f","g","h","j","k","l","m","z","x","y","q");
			$rand_keys = array_rand($input, 3);
			return strtoupper($input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]]);
	   }
   }